package com.my.xq.repository;



import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import com.my.xq.domain.entity.RenWu;
import com.sskj.core.repository.GenericRepository;

public interface RenWuRepository extends GenericRepository<RenWu, String> {

	
	@Query(value = "from RenWu o where o.code=?1")
	RenWu findOneByCode(String code);	
	
	@Query("SELECT o FROM RenWu o " + " where 1=1 AND "
			+ "o.courtId=?1 and o.isActive=true ")			
	RenWu findRenWuActive(String courtId);	
	
	@Query("from RenWu o where o.courtId = ?1 and o.code=?2")
	RenWu findRenWuByCourtANDCode(String courtId,String code);
	
	@Query("from  RenWu  o  where o.courtId = ?1 and o.code=?2")
	public List<RenWu> findRenWuLsByCourt(String courtId,String code);
	

	@Transactional
	@Modifying(clearAutomatically = true)
	@Query("update RenWu o set o.courtId=?1 where o.code=?2")
	void updateRenWu(String courtId,String code);	
	
	@Query("SELECT o FROM RenWu o where 1=1 AND o.isActive=true")			
	List<RenWu> taskFindRenWuActive();
	
	@Query(value = "SELECT court_id FROM sys_ren_wu WHERE court_id IN "
			+ "(SELECT court_id FROM xq_doorplate "
			+ "WHERE proprietor_id = ?1 GROUP BY court_id)"
			+ " and code='CAST01' and is_active=1", nativeQuery = true)			
	List<String> findUnitActiveRenWuByProprietorId(String proprietorId);

	@Query(value = "SELECT RW FROM RenWu RW WHERE RW.courtId = ?1 ORDER BY RW.code")
	List<RenWu> findRenWuByCourtId(String courtId);

	@Query(value = "delete from RenWu where courtId = ?1 ")
	void delByCourtId(String courtId);
}
